package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.FileUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.form.CustomerLimitForm;
import com.feeyin.radar.crm.form.UploadFileForm;
import com.feeyin.radar.crm.service.CustomerCollectService;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.OpenCustomerService;
import com.feeyin.radar.sys.model.Users;
import com.feeyin.radar.sys.service.UserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class CustomerCollectController extends MultiActionController
{
  private CustomerCollectService customerCollectService;
  private OpenCustomerService openCustomerService;
  private UserService userService;
  private CustomerService customerService;
  private String customerCollect;
  private String collectBatchImport;
  private String selectPerson;

  public OpenCustomerService getOpenCustomerService()
  {
    return this.openCustomerService;
  }

  public void setOpenCustomerService(OpenCustomerService openCustomerService) {
    this.openCustomerService = openCustomerService;
  }

  public CustomerCollectService getCustomerCollectService() {
    return this.customerCollectService;
  }

  public void setCustomerCollectService(CustomerCollectService customerCollectService)
  {
    this.customerCollectService = customerCollectService;
  }

  public UserService getUserService() {
    return this.userService;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public String getCustomerCollect() {
    return this.customerCollect;
  }

  public void setCustomerCollect(String customerCollect) {
    this.customerCollect = customerCollect;
  }

  public String getCollectBatchImport() {
    return this.collectBatchImport;
  }

  public void setCollectBatchImport(String collectBatchImport) {
    this.collectBatchImport = collectBatchImport;
  }

  public String getSelectPerson() {
    return this.selectPerson;
  }

  public void setSelectPerson(String selectPerson) {
    this.selectPerson = selectPerson;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));
    map.put("deleteResult", request.getParameter("deleteResult"));

    Page page = new Page();
    page.getPageList(this.customerCollectService.getPageCollectCustomersCount().intValue(), null, request);
    map.put("customers", this.customerCollectService.getPageCollectCustomers(page));

    return new ModelAndView(getCustomerCollect(), map);
  }

  public ModelAndView preSelectPerson(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();

    List users = new ArrayList();
    users = this.userService.getActivityUsers();
    if (users != null) {
      for (int i = 0; i < users.size(); i++) {
        Users user = new Users();
        user = (Users)users.get(i);
        CustomerLimitForm customerLimitForm = new CustomerLimitForm();
        customerLimitForm = this.customerService.getCustomerLimit(user.getId());
        user.setCustomerLimitForm(customerLimitForm);
      }
    }
    map.put("users", users);

    return new ModelAndView(getSelectPerson(), map);
  }

  public ModelAndView salePersonToCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    String salePersonId = request.getParameter("salePersonId");
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    if (!StringUtils.isEmpty(salePersonId)) {
      result = this.customerCollectService.salePersonToCustomer(ids, Integer.valueOf(Integer.parseInt(salePersonId)));
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./customerCollect.do?action=display&result=" + success));
  }

  public ModelAndView preCollectBatchImport(HttpServletRequest request, HttpServletResponse response)
  {
    return new ModelAndView(getCollectBatchImport());
  }

  public ModelAndView collectBatchImport(HttpServletRequest request, HttpServletResponse response, UploadFileForm uploadFileForm)
  {
    Map map = new HashMap();

    MultipartFile multipartFile = uploadFileForm.getContents();
    if (multipartFile == null) {
      request.setAttribute("result", Integer.valueOf(2));
      request.setAttribute("uploadFileForm", uploadFileForm);
      return preCollectBatchImport(request, response);
    }
    if (multipartFile.getSize() > 10000000L) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("uploadFileForm", uploadFileForm);
      return preCollectBatchImport(request, response);
    }

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    List logs = new ArrayList();
    String realPath = getServletContext().getRealPath("/");
    logs = this.openCustomerService.batchImportCustomer(userId, uploadFileForm, realPath, multipartFile, 2);
    map.put("logs", logs);

    return new ModelAndView(getCollectBatchImport(), map);
  }

  public ModelAndView downloadTemplate(HttpServletRequest request, HttpServletResponse response)
  {
    FileUtils fileUtils = new FileUtils();
    fileUtils.downloadCustomerTemplate(getServletContext().getRealPath("/"), response, 2);
    return null;
  }
}